﻿using Oracle.ManagedDataAccess.Client;
using Server.Constant;
using Server.Enum;
using Server.Framework.Message;
using Server.Framework.Processor;
using Server.Framework.Service;
using Server.Framework.Socket;
using Server.Message.Request;
using Server.Message.Response;
using Server.Model;
using Server.OptExp;
using Server.Util;
using System;
using System.Data.Common;
using System.Text;

namespace Server.Processor
{
    /// <summary>
    /// Author: Leil
    /// Description: 请求权限列表
    /// CreateTime: 2020/5/13 10:12:49
    /// </summary>
    [ProcessorAttr(MessageId.REQUEST_MENU_LIST)]
    class RequestMenuListProcessor : IProcessor
    {

        public void Process(ProtobufAppSession session, RequestBaseMessage message)
        {
            if (message is MenuListReq msg)
            {
                StringBuilder conSb = new StringBuilder();
                if (msg.Condition != null)
                {
                    if (msg.Condition.ParentId != 9999)
                    {
                        conSb.Append($"AND PARENT_ID = {msg.Condition.ParentId}");
                    }
                    if (!string.IsNullOrEmpty(msg.Condition.MenuName))
                    {
                        conSb.Append($"AND MENU_NAME LIKE '%{msg.Condition.MenuName}%'");
                    }
                    if (msg.Condition.Status != 0)
                    {
                        conSb.Append($"AND STATUS = {msg.Condition.Status}");
                    }
                }

                int rowCount = CommonTool.GetRowCount("SYS_MENU", conSb.ToString());

                int page = msg.Page;
                int limit = msg.Limit;
                int[] startEnd = CommonTool.ConvertStartEnd(page, limit);
                string sql = $@"SELECT * FROM
(SELECT
	menus.*,
	ROWNUM rn 
FROM
	( SELECT * FROM SYS_MENU WHERE DEL_FLAG = 1 {conSb} ORDER BY SORT DESC ) menus 
WHERE
	ROWNUM <= {startEnd[1]} ) WHERE rn > {startEnd[0]}";
                try
                {
                    DBService.Instance().ExecuteQuery(sql, reader =>
                    {
                        MenuListResp resp = new MenuListResp(MessageId.MENU_LIST)
                        {
                            Count = rowCount
                        };
                        while (reader.Read())
                        {
                            MenuDao menu = new MenuDao()
                            {
                                ID = Convert.ToInt32(reader["ID"]),
                                ParentId = Convert.ToInt32(reader["PARENT_ID"]),
                                Icon = reader["ICON"].ToString(),
                                ScreenCode = reader["SCREEN_CODE"].ToString(),
                                MenuName = reader["MENU_NAME"].ToString(),
                                MenuType = Convert.ToInt32(reader["MENU_TYPE"]),
                                Sort = Convert.ToInt32(reader["SORT"]),
                                Status = Convert.ToInt32(reader["STATUS"]),
                                Creator = reader["CREATOR"].ToString(),
                                CreateTime = reader["CREATE_TIME"].ToString(),
                                Updator = reader["UPDATOR"].ToString(),
                                UpdateTime = reader["UPDATE_TIME"].ToString(),
                            };
                            resp.Menus.Add(menu);
                        }
                        session.Send(resp);
                    });
                }
                catch (CommonOptException e)
                {
                    OperationResp resp = new OperationResp(MessageId.OPT_RESP)
                    {
                        Code = 500,
                        OptType = (int)OptType.QUERY_LIST,
                        Content = e.Message
                    };
                    session.Send(resp);
                }

            }
        }





    }
}
